using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Reflection;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Swagger;

/// <summary>
/// Swagger通用响应文档过滤器
/// </summary>
public class SwaggerCommonResponsesDocumentFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        // 添加通用响应模式定义
        if (!swaggerDoc.Components.Schemas.ContainsKey("ApiErrorResponse"))
        {
            swaggerDoc.Components.Schemas.Add("ApiErrorResponse", new OpenApiSchema
            {
                Type = "object",
                Properties = new Dictionary<string, OpenApiSchema>
                {
                    ["success"] = new OpenApiSchema 
                    { 
                        Type = "boolean", 
                        Example = new Microsoft.OpenApi.Any.OpenApiBoolean(false) 
                    },
                    ["message"] = new OpenApiSchema 
                    { 
                        Type = "string", 
                        Example = new Microsoft.OpenApi.Any.OpenApiString("错误信息") 
                    },
                    ["errorCode"] = new OpenApiSchema 
                    { 
                        Type = "string", 
                        Example = new Microsoft.OpenApi.Any.OpenApiString("1001") 
                    },
                    ["timestamp"] = new OpenApiSchema 
                    { 
                        Type = "string", 
                        Format = "date-time",
                        Example = new Microsoft.OpenApi.Any.OpenApiString("2025-08-08T10:30:00.000Z")
                    },
                    ["traceId"] = new OpenApiSchema 
                    { 
                        Type = "string", 
                        Example = new Microsoft.OpenApi.Any.OpenApiString("req_1234567890") 
                    }
                }
            });
        }

        if (!swaggerDoc.Components.Schemas.ContainsKey("ApiSuccessResponse"))
        {
            swaggerDoc.Components.Schemas.Add("ApiSuccessResponse", new OpenApiSchema
            {
                Type = "object",
                Properties = new Dictionary<string, OpenApiSchema>
                {
                    ["success"] = new OpenApiSchema 
                    { 
                        Type = "boolean", 
                        Example = new Microsoft.OpenApi.Any.OpenApiBoolean(true) 
                    },
                    ["message"] = new OpenApiSchema 
                    { 
                        Type = "string", 
                        Example = new Microsoft.OpenApi.Any.OpenApiString("操作成功") 
                    },
                    ["data"] = new OpenApiSchema 
                    { 
                        Type = "object",
                        Description = "响应数据，具体结构根据接口而定"
                    },
                    ["timestamp"] = new OpenApiSchema 
                    { 
                        Type = "string", 
                        Format = "date-time",
                        Example = new Microsoft.OpenApi.Any.OpenApiString("2025-08-08T10:30:00.000Z")
                    },
                    ["traceId"] = new OpenApiSchema 
                    { 
                        Type = "string", 
                        Example = new Microsoft.OpenApi.Any.OpenApiString("req_1234567890") 
                    },
                    ["pagination"] = new OpenApiSchema
                    {
                        Type = "object",
                        Description = "分页信息（分页查询时提供）",
                        Properties = new Dictionary<string, OpenApiSchema>
                        {
                            ["currentPage"] = new OpenApiSchema 
                            { 
                                Type = "integer", 
                                Example = new Microsoft.OpenApi.Any.OpenApiInteger(1) 
                            },
                            ["pageSize"] = new OpenApiSchema 
                            { 
                                Type = "integer", 
                                Example = new Microsoft.OpenApi.Any.OpenApiInteger(20) 
                            },
                            ["totalCount"] = new OpenApiSchema 
                            { 
                                Type = "integer", 
                                Example = new Microsoft.OpenApi.Any.OpenApiInteger(150) 
                            },
                            ["totalPages"] = new OpenApiSchema 
                            { 
                                Type = "integer", 
                                Example = new Microsoft.OpenApi.Any.OpenApiInteger(8) 
                            },
                            ["hasNext"] = new OpenApiSchema 
                            { 
                                Type = "boolean", 
                                Example = new Microsoft.OpenApi.Any.OpenApiBoolean(true) 
                            },
                            ["hasPrevious"] = new OpenApiSchema 
                            { 
                                Type = "boolean", 
                                Example = new Microsoft.OpenApi.Any.OpenApiBoolean(false) 
                            }
                        }
                    }
                }
            });
        }
    }
}
